package com.summer.safe.config.aop.datafilter;


import com.summer.safe.config.datafilter.TableName;
import com.summer.safe.utils.UserUtils;
import com.summer.safe.system.entity.Role;
import com.summer.safe.system.entity.User;
import com.summer.safe.utils.Global;

import java.util.HashMap;
import java.util.List;

/**
 * 数据过滤
 */
public class DataScope {

    /**
     * 数据过滤
     * @param tNameMap
     * @param type
     * @return
     */

    public static String dataScopeFilter(HashMap<String,String> tNameMap, String type){
        //用户数据过滤
        if(type.equals(Global.USER_DATE)){
            return dataScopeFilter(tNameMap.get(TableName.PRM_OFFICE));
        }
        //部门数据过滤
        if(type.equals(Global.OFFICE_DATE)){
            return OfficedataScopeFilter(tNameMap.get(TableName.PRIMARY));
        }
        return "";
    }


    /**
     * 用户数据过滤
     * @param table
     * @return
     */
    public static String dataScopeFilter(String table){
        User user = UserUtils.getUser();
        StringBuffer stringBuffer = new StringBuffer();
        //超级管理员不通过数据过滤
        if(!user.isAdmin() ) {
            List<Role> roleList = user.getRoleList();
            //拥有角色列表
            if(roleList!=null&&roleList.size()>0) {
                stringBuffer.append("and (");
                for(Role role:roleList){
                    //如果不存在条件则是AND
                        //所有数据
                        if(Role.DATA_SCOPE_ALL.equals(role.getDataScope())){
                            stringBuffer.append(table + ".parent_ids like concat('%','"+user.getOffice().getId()+"','%')");
                            stringBuffer.append(" OR " + table + ".id = '" + user.getOffice().getId()+"'");
                        }//所在部门及以下数据
                        else if(Role.DATA_SCOPE_OFFICE.equals(role.getDataScope())){
                            stringBuffer.append(table + ".parent_ids like concat('%','"+user.getOffice().getId()+"','%')");
                            stringBuffer.append(" OR " + table + ".id = '" + user.getOffice().getId()+"'");
                        }
                        //部门数据
                        else if(Role.DATA_SCOPE_DEPT.equals(role.getDataScope())){
                            stringBuffer.append(table + ".parent_ids like concat('%','"+user.getOffice().getId()+"','%')");
                            stringBuffer.append(" OR " + table + ".id = '" + user.getOffice().getId()+"'");
                        }//自己数据
                        else if(Role.DATA_SCOPE_SELF.equals(role.getDataScope())){
                            stringBuffer.append( "a.id  = '"+user.getId()+"'");
                        }
                        //自定义数据
                        else if(Role.DATA_SCOPE_ENTER.equals(role.getDataScope())){
                            stringBuffer.append("office_id "+" IN ( select office_id from sys_role_office where role_id='" + role.getId()+"' AND office_id!='1' )");
                        }

                }
                stringBuffer.append(")");
            }//没有角色的用户
            else{
                stringBuffer.append(" and a.id ='没有角色' ");
            }
            //如果已存在的角色未找找到过滤方法
            if(stringBuffer.toString().equals("and ()")){
                stringBuffer.setLength(0);
                stringBuffer.append(" and a.id ='未找到过滤方法' ");
            }
        }
        return stringBuffer.toString();
    }


    /**
     *
     * 部门数据过滤
     * @param table
     * @return
     */
    public static String OfficedataScopeFilter(String table){
        User user = UserUtils.getUser();
        StringBuffer stringBuffer = new StringBuffer();
        //超级管理员不通过数据过滤
        if(!user.isAdmin() ) {
            List<Role> roleList = user.getRoleList();
            //拥有角色列表
            if(roleList!=null&&roleList.size()>0) {
                stringBuffer.append("and (");
                for(Role role:roleList){
                    //如果不存在条件则是AND
                        //所有数据
                        if(Role.DATA_SCOPE_ALL.equals(role.getDataScope())){
                            stringBuffer.append(table + ".parent_ids like concat('%','"+user.getOffice().getId()+"','%')");
                            stringBuffer.append(" OR " + table + ".id = '" + user.getOffice().getId()+"'");
                        }//所在部门及以下数据
                        else if(Role.DATA_SCOPE_OFFICE.equals(role.getDataScope())){
                            stringBuffer.append(table + ".parent_ids like concat('%','"+user.getOffice().getId()+"','%')");
                            stringBuffer.append(" OR " + table + ".id = '" + user.getOffice().getId()+"'");
                        }
                        //部门数据
                        else if(Role.DATA_SCOPE_DEPT.equals(role.getDataScope())){
                            stringBuffer.append(table + ".parent_ids like concat('%','"+user.getOffice().getId()+"','%')");
                            stringBuffer.append(" OR " + table + ".id = '" + user.getOffice().getId()+"'");
                        }//个人数据
                        else if(Role.DATA_SCOPE_SELF.equals(role.getDataScope())){
                            stringBuffer.append( "a.create_by = '"+user.getId()+"'");
                        }
                        //自定义数据
                        else if(Role.DATA_SCOPE_ENTER.equals(role.getDataScope())){
                            stringBuffer.append(table+".id "+" IN ( select office_id from sys_role_office where role_id='" + role.getId()+"')");
                        }

                }
                stringBuffer.append(")");
            }//没有角色的用户
            else{
                stringBuffer.append(" and a.id ='没有角色' ");
            }
            //如果已存在的角色未找找到过滤方法
            if(stringBuffer.toString().equals("and ()")){
                stringBuffer.setLength(0);
                stringBuffer.append(" and a.id ='未找到过滤方法' ");
            }
        }
        return stringBuffer.toString();
    }


}
